﻿/********************************************************************************
 * CoTime. All rights reserved.
 * Author: 刘冰
 * Email : ice6@live.cn
 * Description:
 *     针对Model的一些扩展方法
 *     作用是为了让程序看起来更有表达力
*********************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoTime.Model;
using System.IO;

namespace CoTime
{
    public static class ModelHelper
    {
        public static Task GetTaskById(this List<Task> tasks, Int32 id)
        {
            Task task = null;
            if (tasks.Count > 0)
            {
                task = tasks.Find(t => t.Id == id);
            }
            return task ?? new Task(id);
        }

        public static Note GetNoteById(this List<Note> notes, Int32 id)
        {
            Note note = null;
            if (notes.Count > 0)
            {
                note = notes.Find(n => n.Id == id);
            }
            return note ?? new Note(id);
        }

        public static String GetTaskFile(this DateTime dt)
        {
            return Path.Combine(Config.DataPath,
                                            String.Format("Data/{0}/{1}/{2}.xml",
                                            dt.Year,
                                            dt.Month,
                                            dt.ToString("yyyy-MM-dd")
                                            ));
        }

        #region [ Guid ]

        public static string GenGuid()
        {
            long i = 1;
            foreach (byte b in Guid.NewGuid().ToByteArray())
            {
                i *= ((int)b + 1);
            }
            return string.Format("{0:x}", i - DateTime.Now.Ticks);
        }

        #endregion
    }
}
